<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="友链名称" prop="linkName">
      <el-input v-model="dataForm.linkName" placeholder="友链名称"></el-input>
    </el-form-item>
    <el-form-item label="友链logo" prop="linkLogo">
      <el-input v-model="dataForm.linkLogo" placeholder="友链logo"></el-input>
    </el-form-item>
    <el-form-item label="友链地址" prop="linkUrl">
      <el-input v-model="dataForm.linkUrl" placeholder="友链地址"></el-input>
    </el-form-item>
    <el-form-item label="友链状态" prop="linkStatus">
  	    <el-select v-model="dataForm.linkStatus" placeholder="友链状态 0：可用 1：不可用">
					<el-option label="可用" value="0"></el-option>
					<el-option label="不可用" value="1"></el-option>
				</el-select>
    </el-form-item>
    <el-form-item label="是否删除" prop="delFlag">
        <el-select v-model="dataForm.delFlag" placeholder="是否删除 0：存在 1：已删除">
					<el-option label="存在" value="0"></el-option>
					<el-option label="已删除" value="1"></el-option>
				</el-select>
    </el-form-item>
    <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="创建者" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建者"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          linkId: 0,
          linkName: '',
          linkLogo: '',
          linkUrl: '',
          linkStatus: '',
          delFlag: '',
          createTime: '',
          updateTime: '',
          createUser: ''
        },
        dataRule: {
          linkName: [
            { required: true, message: '友链名称不能为空', trigger: 'blur' }
          ],
          linkLogo: [
            { required: true, message: '友链logo不能为空', trigger: 'blur' }
          ],
          linkUrl: [
            { required: true, message: '友链地址不能为空', trigger: 'blur' }
          ],
          linkStatus: [
            { required: true, message: '友链状态 0：可用 1：不可用不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除 0：存在 1：已删除不能为空', trigger: 'blur' }
          ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // updateTime: [
          //   { required: true, message: '更新时间不能为空', trigger: 'blur' }
          // ],
          // createUser: [
          //   { required: true, message: '创建者不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.linkId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.linkId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/weblink/info/${this.dataForm.linkId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.linkName = data.webLink.linkName
                this.dataForm.linkLogo = data.webLink.linkLogo
                this.dataForm.linkUrl = data.webLink.linkUrl
                this.dataForm.linkStatus = data.webLink.linkStatus
                this.dataForm.delFlag = data.webLink.delFlag
                this.dataForm.createTime = data.webLink.createTime
                this.dataForm.updateTime = data.webLink.updateTime
                this.dataForm.createUser = data.webLink.createUser
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/weblink/${!this.dataForm.linkId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'linkId': this.dataForm.linkId || undefined,
                'linkName': this.dataForm.linkName,
                'linkLogo': this.dataForm.linkLogo,
                'linkUrl': this.dataForm.linkUrl,
                'linkStatus': this.dataForm.linkStatus,
                'delFlag': this.dataForm.delFlag,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'createUser': this.dataForm.createUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
